package com.solution.admin.customer.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.solution.admin.customer.dao.CompanyExpressConfigAddressDao;
import com.solution.admin.customer.dao.CompanyExpressConfigDao;
import com.solution.admin.customer.dao.DeliveryAddressDao;
import com.solution.admin.customer.entity.CompanyExpressConfigAddressEntity;
import com.solution.admin.customer.entity.CompanyExpressConfigEntity;
import com.solution.admin.customer.entity.DeliveryAddressEntity;
import com.solution.admin.customer.service.DeliveryAddressService;
import com.solution.common.dao.BaseDao;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.service.impl.BaseServiceImpl;

/**
 * 收货地址表service实现层
 * @author solin
 * @date 2017-09-22 11:55:25
 */
@Service
public class DeliveryAddressServiceImpl extends BaseServiceImpl<DeliveryAddressEntity> implements DeliveryAddressService {

	/**
	 * 收货地址表接口
	 */
	@Autowired
	private DeliveryAddressDao deliveryAddressDao;
	
	@Autowired
	private CompanyExpressConfigDao companyExpressConfigDao;
	
	@Autowired
	private CompanyExpressConfigAddressDao companyExpressConfigAddressDao;

	protected BaseDao<DeliveryAddressEntity> getDao() {
		return this.deliveryAddressDao;
	}

	/**
	 * 保存收货地址表
	 * @param deliveryAddressEntity 收货地址表实体
	 * @return Long 添加成功收货地址表的ID
	 * @author solin
	 * @date 2017-09-22 11:55:25
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor=Exception.class)
	public long saveDeliveryAddress(DeliveryAddressEntity deliveryAddressEntity) {
		if (null != deliveryAddressEntity.getIsDefault() && deliveryAddressEntity.getIsDefault().equals(1)) {
			DeliveryAddressEntity param = new DeliveryAddressEntity();
			param.setCustomerId(deliveryAddressEntity.getCustomerId());
			param.setIsDefault(0);
			param.setModifier(deliveryAddressEntity.getCreater());
			deliveryAddressDao.updateByCustomerId(param);
		}
		//删除该地址免费地址记录
		Map<String, Object> paramMap = new HashMap<String, Object>();
		List<CompanyExpressConfigEntity> configEntityList = companyExpressConfigDao.listBy(paramMap);
		for (CompanyExpressConfigEntity configEntity : configEntityList) {
			if (configEntity.getExpressType() == 2 || configEntity.getExpressType() == 4) {
				//全部免费需要把该地址以外的地址添加到免费列表,并修改该公司策略为部分免费
				//查询除修改地址之外的其他地址
				List<Long> addressIdList = deliveryAddressDao.listOtherAddressIds(deliveryAddressEntity.getId(), deliveryAddressEntity.getCompanyId());
				List<CompanyExpressConfigAddressEntity> configAddressList = new ArrayList<CompanyExpressConfigAddressEntity>();
				//查询所有地址列表
				for (Long addressId : addressIdList) {
					CompanyExpressConfigAddressEntity entity = new CompanyExpressConfigAddressEntity();
					entity.setAddressId(addressId);
					entity.setCompanyId(deliveryAddressEntity.getCompanyId());
					entity.setExpressConfigId(configEntity.getId());
					configAddressList.add(entity);
				}
				//删除已经存在的部分免运费地址
				paramMap.put("companyId", deliveryAddressEntity.getCompanyId());
				paramMap.put("expressConfigId", configEntity.getId());
				companyExpressConfigAddressDao.removeFreeAddressInfo(paramMap);
				//重新保存新的部分免运费地址列表
				companyExpressConfigAddressDao.insert(configAddressList);
				if (configEntity.getExpressType() == 2) {
					configEntity.setExpressType(1);
				} else if (configEntity.getExpressType() == 4) {
					configEntity.setExpressType(3);
				}
				//修改物流配置为部分免运费
				companyExpressConfigDao.update(configEntity);
			}
		}
		return deliveryAddressDao.saveDeliveryAddress(deliveryAddressEntity);
	}

	/**
	 * 分页查询收货地址表
	 * @param pageParam 分页参数
	 * @param paramMap 查询参数
	 * @return PageBean 分页参数和分页数据
	 * @author solin
	 * @date 2017-09-22 11:55:25
	 */
	@Override
	public PageBean listDeliveryAddressPage(PageParam pageParam, Map<String, Object> paramMap) {
		return deliveryAddressDao.listDeliveryAddressPage(pageParam, paramMap);
	}
	
	/**
	 * 根据ID查询用户信息
	 * @param id 用户id
	 * @return deliveryAddressEntity 用户对象
	 * @author solin
	 * @date 2017-09-22 11:55:25
	 */
	public DeliveryAddressEntity getDeliveryAddressById(Long id) {
		return deliveryAddressDao.getById(id.longValue());
	}

	/**
	 * 更新用户
	 * @param deliveryAddressEntity 用户实体
	 * @return Long 更新是否成功，>1代表更新成功
	 * @author solin
	 * @date 2017-09-22 11:55:25
	 */
	@Override
	@Transactional(rollbackFor=Exception.class)
	public int updateDeliveryAddress(DeliveryAddressEntity deliveryAddressEntity) {
		if (null != deliveryAddressEntity.getIsDefault() && deliveryAddressEntity.getIsDefault() == 1) {
			DeliveryAddressEntity param = new DeliveryAddressEntity();
			param.setCustomerId(deliveryAddressEntity.getCustomerId());
			param.setIsDefault(0);
			deliveryAddressDao.updateByCustomerId(param);
		}
		//删除该地址免费地址记录
		Map<String, Object> paramMap = new HashMap<String, Object>();
		List<CompanyExpressConfigEntity> configEntityList = companyExpressConfigDao.listBy(paramMap);
		for (CompanyExpressConfigEntity configEntity : configEntityList) {
			if (configEntity.getExpressType() == 1 || configEntity.getExpressType() == 3) {
				//部分免费只去除对应地址
				paramMap.put("addressId", deliveryAddressEntity.getId());
				paramMap.put("companyId", deliveryAddressEntity.getCompanyId());
				paramMap.put("expressConfigId", configEntity.getId());
				companyExpressConfigAddressDao.removeFreeAddressInfo(paramMap);
			} else {
				//全部免费需要把该地址以外的地址添加到免费列表,并修改该公司策略为部分免费
				//查询除修改地址之外的其他地址
				List<Long> addressIdList = deliveryAddressDao.listOtherAddressIds(deliveryAddressEntity.getId(), deliveryAddressEntity.getCompanyId());
				List<CompanyExpressConfigAddressEntity> configAddressList = new ArrayList<CompanyExpressConfigAddressEntity>();
				//查询所有地址列表
				for (Long addressId : addressIdList) {
					CompanyExpressConfigAddressEntity entity = new CompanyExpressConfigAddressEntity();
					entity.setAddressId(addressId);
					entity.setCompanyId(deliveryAddressEntity.getCompanyId());
					entity.setExpressConfigId(configEntity.getId());
					configAddressList.add(entity);
				}
				//删除已经存在的部分免运费地址
				paramMap.put("companyId", deliveryAddressEntity.getCompanyId());
				paramMap.put("expressConfigId", configEntity.getId());
				companyExpressConfigAddressDao.removeFreeAddressInfo(paramMap);
				//重新保存新的部分免运费地址列表
				companyExpressConfigAddressDao.insert(configAddressList);
				if (configEntity.getExpressType() == 2) {
					configEntity.setExpressType(1);
				} else if (configEntity.getExpressType() == 4) {
					configEntity.setExpressType(3);
				}
				//修改物流配置为部分免运费
				companyExpressConfigDao.update(configEntity);
			}
		}
		return deliveryAddressDao.updateDeliveryAddress(deliveryAddressEntity);
	}
	
	
	/**
	 * 根据条件查询收货地址表
	 * @param param
	 * @return 数据列表
	 * @author solin
	 * @date 2017-09-22 11:55:25
	 */
	@Override
	public List<DeliveryAddressEntity> listDeliveryAddress( Map<String, Object> param){
		return  deliveryAddressDao.listDeliveryAddress(param);
	}
	
	/**
	 * 删除收货地址表
	 * @param 
	 * @return int 返回删除记录条数
	 * @author solin
	 * @date 2017-09-22 11:55:25
	 */
	public int deleteDeliveryAddressById(long id){
		return  deliveryAddressDao.deleteById(id);
	}

	/**
	 * 设置默认地址修改专用
	 * @param deliveryAddressEntity
	 * @return
	 * @author xkl
	 * @date 2018年3月25日 上午11:44:01
	 */
	@Override
	public int setDefaultAddress(DeliveryAddressEntity deliveryAddressEntity) {
		if (null != deliveryAddressEntity.getIsDefault() && deliveryAddressEntity.getIsDefault() == 1) {
			DeliveryAddressEntity param = new DeliveryAddressEntity();
			param.setCustomerId(deliveryAddressEntity.getCustomerId());
			param.setIsDefault(0);
			deliveryAddressDao.updateByCustomerId(param);
		}
		return deliveryAddressDao.updateDeliveryAddress(deliveryAddressEntity);
	}
	
}
